package com.imooc.aspect;

import jdk.nashorn.internal.ir.CallNode;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Aspect
@Component
public class HttpAspect {

    private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);

    @Pointcut("execution(public * com.imooc.controller.GirlController.*(..))")
    public void log() {
    }

    @Before("log()")
    public void doBefore(JoinPoint joinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request =  attributes.getRequest();
       // url路径
        logger.info("请求url：{}", request.getRequestURL());

        // 请求方法
        logger.info("请求方法：{}", request.getMethod());

        // IP
        logger.info("请求IP={}", request.getRemoteAddr());

        //请求类方法
        logger.info("调用方法: {}", joinPoint.getSignature().getDeclaringTypeName() + "."  + joinPoint.getSignature().getName());

        //请求参数
        logger.info("请求参数：{}", joinPoint.getArgs());

    }

//    @After("log()")
//    public void doAfter(){
//        logger.info("22222222");
//    }

    @AfterReturning(returning = "objects", pointcut = "log()")
    public void doAfterReturning(Objects objects){
        logger.info("response: {}", objects.toString());
    }
}
